import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.beanutils.BeanUtils;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

public class ToolsOfMoney {
    private final static    Connection  connection;
    //德鲁伊连接池
 static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src/bankPoketMoney.properties"));
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            connection = dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //封装预处理
    public  static PreparedStatement getPreparedStatement(String sql, Connection connection, Object[] parameters) throws Exception {
        PreparedStatement statement = connection.prepareStatement(sql);
        ParameterMetaData parameterMetaData = statement.getParameterMetaData();
        int count = parameterMetaData.getParameterCount();
        //3,判断是否传参，并设置参数
        if (count != 0 && parameters != null && parameters.length == count) {
            for (int i = 0; i < count; i++) {
                statement.setObject(i + 1, parameters[i]);

            }

        }
        return statement;
    }

    public static int insert(String sql,Object...paremeters) throws Exception {

        //使用封装的德鲁伊连接池方法
        PreparedStatement preparedStatement = ToolsOfMoney.getPreparedStatement(sql, connection, paremeters);
        int resultSet1 = preparedStatement.executeUpdate();



        int i1 = preparedStatement.executeUpdate();
        return i1;
    }
    public static <T> List<T> selectlistReflect(String sql, Class<T> cls, Object...parameters) throws Exception {
        //定义需要的变量

        PreparedStatement preparedStatement;
        ResultSet resultSet;
        T t=null;
        ArrayList<T> arrayList = new ArrayList<>();//多条查询需要定义集合
        //获取链接
        preparedStatement = ToolsOfMoney.getPreparedStatement(sql, connection, parameters);
         resultSet = preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        while (resultSet.next()){
            t = cls.newInstance();//实例化一个bean对象，用来封装数据
            int count = metaData.getColumnCount();//获得字段/列数
            for (int i = 1; i < count+1; i++) {//字段数是从1开始，所以i一定要定义为1
                BeanUtils.setProperty(t,metaData.getColumnName(i),resultSet.getObject(i));//jar包封装好的方法
                /**
                 * BeanUtils.setProperty(t【A】,metaData.getColumnName(i)【B】,resultSet.getObject(i)【C】)
                 *将【B】【C】添加到【A】
                 */
            }
            arrayList.add(t);//将单条对象添加到集合里

        }

        return arrayList;
    }


    /**
     * 查
     */
    public static <T> T selectData(String sql,Class<T> cls,Object...parameters) throws Exception {
        T t = null;
        //使用封装的德鲁伊连接池方法
        PreparedStatement preparedStatement = ToolsOfMoney.getPreparedStatement(sql, connection, parameters);
        ResultSet resultSet1 = preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet1.getMetaData();
        int i = 0;
        while (resultSet1.next()) {
            t = cls.newInstance();
            int count1 = metaData.getColumnCount();
            System.out.println(count1);
            for (i = 1; i < count1+1; i++) {

                BeanUtils.setProperty(t, metaData.getColumnName(i), resultSet1.getObject(i));

            }
        }
        if (t != null) {
            System.out.println("查询成功");
        } else {
            System.out.println("查询失败");
        }
        return t;

    }

    /**
     * 更新
     */
    public static int updateData(String sql,Object...paremeters) throws Exception {
        PreparedStatement preparedStatement = ToolsOfMoney.getPreparedStatement(sql,connection,paremeters);
        int i1 = preparedStatement.executeUpdate();
        if (i1!=0){
            System.out.println("更新成功");
        }else{
            System.out.println("更新失败");
        }
        return i1;
    }
    /**
     * 登录
     */
    public void logon() throws Exception {
        money_javabean moneyJavabean = new money_javabean();
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入您的用户名");
        String name = scanner.next();
        System.out.println("请输入您的密码");
        String password = scanner.next();

        String sql = "select * from money where name=? && password=?";
        //4.创建数据操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setString(2, password);
        //5.执行sql，获取结果集
        ResultSet resultSet = statement.executeQuery();

        if (resultSet.next()){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败,请重新登录");
            ToolsOfMoney toolsOfMoney = new ToolsOfMoney();
            toolsOfMoney.logon();

        }


}
}
